<?php
/*
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 * Update: NghiaTruong
 * Email: nghiatruong@vietnambiz.com
 * Date: 16-3-2011
 */
class comment{

    function comment(){}

    function getAllArticleHaveComment(){
        global $db,$config;
        $sqlTotalRows="SELECT a.id
            FROM article a
            RIGHT JOIN comment c ON a.id=c.aid
            GROUP BY a.id
            ORDER BY a.create_date DESC";
        $arrTotalRows =  $db->query($sqlTotalRows);
        $totalRows = $db->num_rows($arrTotalRows);
        $totalPages = ceil($totalRows/$config ['total_article_on_admin']);
        $start = (($config ['page']*$config ['total_article_on_admin'])-$config ['total_article_on_admin']);
        $limit = $config ['total_article_on_admin'];
        error_reporting(1);
        $sql="SELECT a.id as article_id,a.title,a.link,a.create_date,
            (select count(*) from comment where aid=a.id and status='yes') as num_comment_active,
            (select count(*) from comment where aid=a.id and status='no') as num_comment_notactive
            FROM article a
            RIGHT JOIN comment c ON a.id=c.aid
            GROUP BY a.id
            ORDER BY a.last_comment DESC, a.create_date DESC LIMIT $start,$limit";
        $rsAllComment = $db->query($sql);
        return array('totalPages'=>$totalPages,'rs'=>$rsAllComment);
    }

    function getAllCommentByArticle($aid){
        $aid = intval($aid);
        global $db;
        $sql="SELECT c.* FROM comment c WHERE c.aid=$aid ORDER BY c.id";
        $rsAllComment = $db->query($sql);
        return $rsAllComment;
    }

    function get_cat_tree($parent_id = 0,$space = ' ', $trees = NULL){
        if(!$trees) $trees = array();
        $sql = mysql_query("SELECT * FROM article_category WHERE  parent_id = ".intval($parent_id));
        while($rs = mysql_fetch_assoc($sql)){
            if($parent_id==0){
                $trees[] = array('id'=>$rs['id'],'name'=>$rs['name']);
            }else{
                $trees[] = array('id'=>$rs['id'],'name'=>$space.$rs['name']);
            }

            $trees = $this->get_cat_tree($rs['id'],$space.'----',$trees);
        }
        return $trees;
    }


    function commentAction(){
        global $db,$config;
        if(isset($_REQUEST['commentActionType'])){
            $actionType = $_REQUEST['commentActionType'];
            switch ($actionType) {

                case 'commentActionType':
                    if(isset($_GET['id'])){
                        $id = intval($_GET['id']);
                        $this->updateArticleComment($id);
                    }else{
    //                    getAllCategory();
                    }
                    break;
                case 'updateStatus':
                    if(isset($_POST['id'])){
                        $id = intval($_POST['id']);
                        $this->updateArticleComment($id);
                    }else{
    //                    getAllCategory();
                    }
                    break;
                case 'reviewComment':
                    $id = intval($_REQUEST['id']);
                    if($id>0){
                        $rs = $db->query_first("SELECT id,comment FROM comment WHERE id=$id");
                        if($rs['status']=='yes') {
                            echo "<img style=\"cursor:pointer\" onclick=\"javascript: toggle('".$rs['id']."',this,1);\" height=\"16\" width=\"16\" title=\"Comment is actived!\" src=\"".$config ['admin_images_url']."/icons/circle-green.png\"/>";
                        }else {
                            echo "<img style=\"cursor:pointer\" onclick=\"javascript: toggle('".$rs['id']."',this,1);\" height=\"16\" width=\"16\" title=\"Comment is not actived!\" src=\"".$config ['admin_images_url']."/icons/circle-red.png\"/>";
                        }
                        echo $rs['comment'];
                    }else{
                        echo 'Error!';
                    }
                    die;
                    break;

                case 'deleteComment':
                    if(isset($_POST['id'])){
                        $this->deleteComment($_POST['id']);
                    }else{
    //                    getAllComment();
                    }
                    break;
                default:
    //                getAllComment();
            }
        }
    }
    function deleteComment($id){
        global $db;
        $deleteSql = "DELETE FROM comment WHERE id = ".intval($id);
        $db->query($deleteSql);
        $_SESSION['rsMessages'] = "Delete comment successfull!";
        die;
    }
    function deleteAllComment($id){
        global $db;
        $deleteSql = "DELETE FROM comment WHERE aid = ".intval($id);
        $db->query($deleteSql);
        $_SESSION['rsMessages'] = "Delete comment successfull!";
        die;
    }
    function updateArticleComment($id) {
        global $db;
        $rsCheck = $db->query_first("SELECT status FROM comment WHERE id=".intval($id));
        if($rsCheck['status']=='yes') {
            $db->query_first("UPDATE comment SET status='no' WHERE id=".intval($id));
            echo 'no';die;
        }elseif($rsCheck['status']=='no') {
            $db->query_first("UPDATE comment SET status='yes' WHERE id=".intval($id));
            echo 'yes';die;
        }else {
            echo 'hack';die;
        }
    }
}
?>
